﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using XS.Tunnel.Cad.Cmd.Dto;
using XS.Tunnel.Cad.Cmd.Helper;
using XS.Tunnel.Cad.SteelBracing.Infrastructure;
using XS.Tunnel.Cad.SteelBracing.Infrastructure.Extensions;

namespace XS.Tunnel.Cad.Cmd.Data
{
    public interface IBaseApiService
    {


    }


    public class BaseApiService : IBaseApiService
    {
        private string Token = "";
        public BaseApiService()
        {
            var d = Login(new LoginInput { account= "admin", password= "4297f44b13955235245b2497399d7a93" });
            Token = d.token;
        }

        public LoginOutput Login(LoginInput input)
        {
            var result = Post<LoginOutput>("api/client/auth/login", input);

            return result.data;
        }

        public virtual ApiResult<T> Get<T>(string url) where T : class, new()
        {
            try
            {
                Dictionary<string, string> header = null;

                header = new Dictionary<string, string>();
                header.Add("Authorization", Token);

                var result = HttpHelper.Get(ConfigSetting.ApiSite + url, header).ToObject<ApiResult<T>>();
                if (result == null)
                {
                    return null;
                }
                if (result.code != 200)
                {
                    throw XSException.Oh("请求出错", $"请求url{url}", result.code);
                }
                return result;
            }
            catch (Exception ex)
            {
                throw XSException.Oh($"{url}{ex}", ex.ToString());

            }

        }

        public virtual ApiResult<T> Post<T>(string url, LoginInput postData) where T : class, new()
        {
            Dictionary<string, string> header = null;
          
            var resultStr = HttpHelper.Post(ConfigSetting.ApiSite + url, postData.ToJson(), header);
            if (string.IsNullOrEmpty(resultStr))
            {
                return null;
            }
            var result = resultStr.ToObject<ApiResult<T>>();
            if (result == null)
            {
                return null;
            }
            if (result.code != 200)
            {
                throw XSException.Oh("请求出错", $"请求url{url}", result.code);
            }
            return result;
        }


    }

  

    public class BaseProjectApiService : BaseApiService
    {

        public BaseProjectApiService()
        {
            //TODO:项目id设置

            MajorPrjectId = "306995278794196229";



        }

        public string MajorPrjectId { get; set; }

    }

}
